package projectmanagement.console;

import projectmanagement.managementtool.ManagementToolServices.*;

import java.util.Scanner;

/**
 * @author Dmitry Toryanik
 *         <p/>
 *         Console manager class handles and renders
 *         main menu's options. Has a custom constructor which
 *         takes an interface that contains definition of
 *         methods to work with business logic of application.
 */
public class ConsoleManager {

    private boolean continueCurrent = true;
    private ManagementTool managementTool;

    public ConsoleManager(ManagementTool managementTool) {
        this.managementTool = managementTool;
    }

    public void start() {
        final ConsoleMenu currentMenu = new ConsoleMenu();

        currentMenu.add("Projects", new ConsoleMenuCallback() {
            public void invoke() {
                projectsMenuHandler(currentMenu);
            }
        });
        currentMenu.add("Users", new ConsoleMenuCallback() {
            public void invoke() {
                usersMenuHandler(currentMenu);
            }
        });
        currentMenu.add("Scrum info", new ConsoleMenuCallback() {
            public void invoke() {
                scrumInfoMenuHandler(currentMenu);
            }
        });
        currentMenu.add("Exit", new ConsoleMenuCallback() {
            public void invoke() {
                exitHandler();
            }
        });

        while (continueCurrent) {
            currentMenu.show("\"Jira v.0.9\"");
        }

        System.exit(0);
    }

    void projectsMenuHandler(final ConsoleMenu cameFromMenu) {
        final ConsoleMenu currentMenu = new ConsoleMenu();
        final ProjectsHandling projectsHandling = new ProjectsHandling(this);

        currentMenu.add("Create project from scratch", new ConsoleMenuCallback() {
            public void invoke() {
                projectsHandling.createProjectFromScratch();
            }
        });
        currentMenu.add("Go to existing project", new ConsoleMenuCallback() {
            public void invoke() {
                projectsHandling.goToProject();
            }
        });
        currentMenu.add("Show entire list of projects", new ConsoleMenuCallback() {
            public void invoke() {
                projectsHandling.showProjectsList();
            }
        });
        currentMenu.add("Go back", new ConsoleMenuCallback() {
            public void invoke() {
                if (cameFromMenu == null) start();
                cameFromMenu.show("\"Jira v.0.9\"");
            }
        });
        currentMenu.show("Projects");
    }

    void usersMenuHandler(final ConsoleMenu cameFromMenu) {
        final ConsoleMenu currentMenu = new ConsoleMenu();
        final UsersHandling usersHandling = new UsersHandling(this);

        currentMenu.add("Create user", new ConsoleMenuCallback() {
            public void invoke() {
                usersHandling.createUser();
            }
        });
        currentMenu.add("Go to existing user", new ConsoleMenuCallback() {
            public void invoke() {
                usersHandling.goToUser();
            }
        });
        currentMenu.add("Show entire list of users", new ConsoleMenuCallback() {
            public void invoke() {
                usersHandling.showUsers();
            }
        });
        currentMenu.add("Go back", new ConsoleMenuCallback() {
            public void invoke() {
                if (cameFromMenu == null) start();
                cameFromMenu.show("\"Jira v.0.9\"");
            }
        });
        currentMenu.show("Users");
    }

    void scrumInfoMenuHandler(final ConsoleMenu cameFromMenu) {
        final ConsoleMenu currentMenu = new ConsoleMenu();
        final ScrumHandling scrumHandling = new ScrumHandling(this);

        currentMenu.add("Show project iterations", new ConsoleMenuCallback() {
            public void invoke() {
                scrumHandling.showProjectIterations();
            }
        });
        currentMenu.add("Show project tasks", new ConsoleMenuCallback() {
            public void invoke() {
                scrumHandling.showProjectTasks();
            }
        });
        currentMenu.add("Go back", new ConsoleMenuCallback() {
            public void invoke() {
                if (cameFromMenu == null) start();
                cameFromMenu.show("\"Jira v.0.9\"");
            }
        });
        currentMenu.show("Scrum information");
    }

    private void exitHandler() {
        continueCurrent = false;
    }

    static void goBackHandler(String text, Scanner scanner) {
        System.out.printf("%s\nPress enter to continue...", text);
        scanner.nextLine();
    }

    public ManagementTool getManagementTool() {
        return managementTool;
    }
}
